package com.winhxd.b2c.customer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.common.PagedCondition;
import com.winhxd.b2c.common.domain.customer.condition.DeliveryInfoCondition;
import com.winhxd.b2c.common.domain.customer.enums.DeliveryInfoEnum;
import com.winhxd.b2c.common.domain.customer.model.CustomerDeliveryInfo;
import com.winhxd.b2c.common.domain.system.user.model.SysUser;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.system.UserServiceClient;
import com.winhxd.b2c.customer.dao.CustomerDeliveryInfoMapper;
import com.winhxd.b2c.customer.service.DeliveryInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: sunwenwu
 * @Date: 2018/11/8 14：39
 * @Description: 小程序用户收货地址服务接口类
 */
@Service
public class DeliveryInfoServiceImpl implements DeliveryInfoService {
    private static final Logger logger = LoggerFactory.getLogger(DeliveryInfoServiceImpl.class);

    @Autowired
    private CustomerDeliveryInfoMapper customerDeliveryInfoMapper;

    @Autowired
    private UserServiceClient userServiceClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeliveryInfo(DeliveryInfoCondition condition) {

        //参数校验
        validateCondition(condition);
        CustomerDeliveryInfo customerDeliveryInfo = new CustomerDeliveryInfo();
        ResponseResult<SysUser> result = userServiceClient.getSysUserById(condition.getSysUserId());
        SysUser user = result.getData();
        if (null == condition.getId()) {
            //新增操作
            BeanUtils.copyProperties(condition,customerDeliveryInfo);
            customerDeliveryInfo.setCreated(new Date());
            customerDeliveryInfo.setCreatedBy(condition.getSysUserId());
            customerDeliveryInfo.setCreatedName(user.getUsername());
            customerDeliveryInfo.setStatus(0);
            customerDeliveryInfo.setDeleteFlag(0);
            customerDeliveryInfoMapper.insertSelective(customerDeliveryInfo);
        }else {
            //更新操作
            BeanUtils.copyProperties(condition,customerDeliveryInfo);
            customerDeliveryInfo.setUpdated(new Date());
            customerDeliveryInfo.setUpdatedBy(condition.getSysUserId());
            customerDeliveryInfo.setUpdatedName(user.getUsername());
            customerDeliveryInfoMapper.updateByPrimaryKeySelective(customerDeliveryInfo);
        }
        return true;
    }

    @Override
    public Boolean deleteDeliveryInfo(DeliveryInfoCondition condition) {
        if (null == condition) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getId()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        CustomerDeliveryInfo customerDeliveryInfo = new CustomerDeliveryInfo();
        ResponseResult<SysUser> result = userServiceClient.getSysUserById(condition.getSysUserId());
        SysUser user = result.getData();
        customerDeliveryInfo.setDeleteFlag(1);
        customerDeliveryInfo.setId(condition.getId());
        customerDeliveryInfo.setUpdated(new Date());
        customerDeliveryInfo.setUpdatedBy(condition.getSysUserId());
        customerDeliveryInfo.setUpdatedName(user.getUsername());
        customerDeliveryInfoMapper.updateByPrimaryKeySelective(customerDeliveryInfo);
        return true;
    }

    @Override
    public Boolean unusedDeliveryInfo(DeliveryInfoCondition condition) {
        if (null == condition) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getId()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getStatus()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        CustomerDeliveryInfo customerDeliveryInfo = new CustomerDeliveryInfo();
        ResponseResult<SysUser> result = userServiceClient.getSysUserById(condition.getSysUserId());
        SysUser user = result.getData();
        if (DeliveryInfoEnum.EFFICTIVE.getCode().equals(condition.getStatus())) {
            customerDeliveryInfo.setStatus(DeliveryInfoEnum.VALIDATE.getCode());
        }
        if (DeliveryInfoEnum.VALIDATE.getCode().equals(condition.getStatus())) {
            customerDeliveryInfo.setStatus(DeliveryInfoEnum.EFFICTIVE.getCode());
        }
        customerDeliveryInfo.setId(condition.getId());
        customerDeliveryInfo.setUpdated(new Date());
        customerDeliveryInfo.setUpdatedBy(condition.getSysUserId());
        customerDeliveryInfo.setUpdatedName(user.getUsername());
        customerDeliveryInfoMapper.updateByPrimaryKeySelective(customerDeliveryInfo);
        return true;
    }

    @Override
    public List<CustomerDeliveryInfo> findDeliveryInfo() {
        return customerDeliveryInfoMapper.selectAllDeliveryInfo();
    }

    @Override
    public PagedList<CustomerDeliveryInfo> findDeliveryInfoTOPage(PagedCondition condition) {
        PagedList<CustomerDeliveryInfo> listPagedList = new PagedList<CustomerDeliveryInfo>();
        Page page = PageHelper.startPage(condition.getPageNo(),condition.getPageSize(),condition.getOrderBy());
        Page<CustomerDeliveryInfo> infoList = customerDeliveryInfoMapper.selectAllDeliveryInfoToPage(condition);
        for (CustomerDeliveryInfo customerDeliveryInfo : infoList) {
            customerDeliveryInfo.setStatusDesc(DeliveryInfoEnum.getDescMap().get(customerDeliveryInfo.getStatus()));
        }

        listPagedList.setData(infoList);
        listPagedList.setPageNo(condition.getPageNo());
        listPagedList.setPageSize(condition.getPageSize());
        listPagedList.setTotalRows(page.getTotal());
        return listPagedList;
    }

    @Override
    public CustomerDeliveryInfo findDeliveryInfoById(Long id) {
        return customerDeliveryInfoMapper.selectByPrimaryKey(id);
    }

    private void validateCondition(DeliveryInfoCondition condition) {
        //判断必填参数
        if (null == condition) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getExpressCode()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getExpressName()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        if (null == condition.getSysUserId()) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
    }
}
